package com.alm.token.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import butterknife.ButterKnife;

/**
 * Created by lidengzhou on 16/8/17.
 * 该基类实现了当fragment可见的时候才加载数据(View已经预加载)
 */
public abstract class BaseViewPageFragment extends BaseFragment{

    protected boolean isViewInitiated;
    protected boolean isVisibleToUser;
    protected boolean isDataInitiated;



    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        prepareLoadData();
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        //请注意,并没有走BaseFragment的onViewCreated()流程
        unbinder = ButterKnife.bind(this, view);
        behavior = new SimpleBehavior(this);
        isDataInitiated = false;
        preenter=initPreenter();
        initVariables();
        initViews(view);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated = true;
        prepareLoadData();
    }

    public boolean prepareLoadData() {
        return prepareLoadData(false);
    }

    public boolean prepareLoadData(boolean forceUpdate) {
        if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
            loadData();
            isDataInitiated = true;
            return true;
        }
        return false;
    }
}
